﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Text.RegularExpressions;

namespace HHMMS.WorkFlow
{
    public static class WFUtil
    {
        /// <summary>
        /// 在页面底部注册alert脚本。
        /// </summary>
        /// <param name="page"></param>
        /// <param name="message">提示信息</param>
        /// <param name="close">是否关闭当前窗口</param>
        /// <param name="refreshParent">是否刷新父级窗口</param>
        /// <param name="url">当前页面跳转到的url</param>
        public static void MessageBox(this System.Web.UI.Page page, string message, bool close = true, bool refreshParent = true, string url = null)
        {
            var scripts = new StringBuilder();
            scripts.AppendFormat("alert(\"{0}\");", message);
            if (refreshParent)
            {
                scripts.Append("try { window.opener.location.href = window.opener.location.href; } catch(e) {};");
                scripts.Append("try { window.parent.location.href = window.parent.location.href; } catch(e) {};");
            }
            if (!string.IsNullOrEmpty(url))
            {
                scripts.AppendFormat("window.location.href = \"{0}\";", url);
            }
            if (close)
            {
                scripts.Append("window.opener = null;");
                scripts.Append("window.open('','_self');");
                scripts.Append("window.close();");
            }
            page.ClientScript.RegisterStartupScript(page.GetType(), "messageBox", scripts.ToString(), true);
        }
    }
}
